import { createApp } from 'vue'
import App from '@/App.vue'
import router from '@/router'
import { store, key } from './store'
import '@/styles/base.scss'
// import 'element-plus/dist/index.css'
import { getUserInfo, cleanSession } from '@/global/session'
//引入图标
import * as Icons from '@element-plus/icons-vue'
const app = createApp(App)
app.use(router).use(store, key).mount('#app')
//全局注册图标组件 
Object.keys(Icons).forEach((key) => {
  app.component(key, Icons[key as keyof typeof Icons])
})
// 全局变量
if (import.meta.env.MODE === 'development') {
  //dev运行https://zdyuc.douziit.com  http://salestest.douziit.com
  app.config.globalProperties.$baseURL = 'http://salestest.douziit.com'
  app.config.globalProperties.$uploadFile='/api'
} else {
  app.config.globalProperties.$baseURL = ''
  app.config.globalProperties.$uploadFile=''
}
//路由守卫
//权限验证处理:全集守卫路由实现
const whiteList = ['/login']; //白名单
router.beforeEach(async (to, from, next) => {
  //获取token
  const userInfo = getUserInfo()
  const token = userInfo ? JSON.parse(userInfo).token : ''
  window.document.title = to.meta.title ? to.meta.title as string : '豆子信息'
  if (token) {
    store.commit('loading/setLoad',false)
    store.dispatch('http/getHttpList')//切换页面 中端上页面请求
    next()
  } else {
    //判断是否在白名单中
    if (whiteList.indexOf(to.path) !== -1) { //存在白名单，放行
      store.commit('loading/setLoad',false)
      store.dispatch('http/getHttpList')//切换页面 中端上页面请求
      next()
    } else { //不存在，登录
      cleanSession()
      router.replace({
        name: 'login',
        // 登录成功后跳入浏览的当前页面
        query: { redirect: router.currentRoute.value.fullPath }
      })
    }
  }
})
